package cn.sytton.taffe.framework.factory;

//import net.coobird.thumbnailator.Thumbnails;
//import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;

/**
 * 图片处理工厂类
 *
 * @author skyrock
 */
@Component
public class ImageUtil {

    private static final String DEFAULT_PATH = "images";

    @Resource
    private FileUtil fileUtil;

    @Value("${image.path:}")
    private String rootPath;

    /**
     * 获取图片文件的根目录
     * @return 根目录
     */
    public String getImageRootPath() {
        return fileUtil.getRootPath(rootPath, DEFAULT_PATH);
    }

    /**
     * 获取照片头像目录
     * @return 目录
     */
    public File getPhotoPath() {
        String root = getImageRootPath();
        File photoPath = new File(root, "photo");
        if (!photoPath.exists()) {
            if (photoPath.mkdirs()) {
                return photoPath;
            }
        }
        return null;
    }

    /**
     * 获取照片头像目录 虚拟用户
     * @return 目录
     */
    public File getUserPhotoPath() {
        File photoPath = getPhotoPath();
        File userPhotoPath = new File(photoPath, "user");
        if (!userPhotoPath.exists()) {
            if (userPhotoPath.mkdirs()) {
                return userPhotoPath;
            }
        }

        return null;
    }

    /**
     * 裁剪成正方形图片
     * @param fromPic 图片文件
     * @param size 裁剪尺寸
     */
    public static void ClipSquare(File fromPic, int size){
//        try {
//            if (fromPic == null) return;
//            BufferedImage image = ImageIO.read(fromPic);
//            if (image == null) return;
//            ByteArrayOutputStream tmpFile = new ByteArrayOutputStream();
//            Thumbnails.Builder<BufferedImage> builder;
//            int imageWidth = image.getWidth();
//            int imageHeight = image.getHeight();
//            if (imageWidth != imageHeight) {
//                if (imageWidth > imageHeight) {
//                    Thumbnails.of(fromPic).scale(getScale(size, imageHeight)).toOutputStream(tmpFile);
//                    InputStream is = new ByteArrayInputStream(tmpFile.toByteArray());
//
//                    image = Thumbnails.of(is).height(size).asBufferedImage();
//                    builder = Thumbnails.of(image).sourceRegion(Positions.CENTER, size, size).size(size, size);
//                } else {
//                    Thumbnails.of(fromPic).scale(getScale(size,imageWidth)).toOutputStream(tmpFile);
//                    InputStream is = new ByteArrayInputStream(tmpFile.toByteArray());
//
//                    image = Thumbnails.of(is).width(size).asBufferedImage();
//                    int offset = size/10; //避免截到头部,偏移1/10
//                    builder = Thumbnails.of(image).sourceRegion(0, offset, size, size).size(size, size);
//                }
//            } else {
//                Thumbnails.of(fromPic).scale(getScale(size,imageWidth)).toOutputStream(tmpFile);
//                InputStream is = new ByteArrayInputStream(tmpFile.toByteArray());
//
//                image = Thumbnails.of(is).width(size).asBufferedImage();
//                builder = Thumbnails.of(image).size(size, size);
//            }
//            String fileName = fromPic.getName();
//            String newFileName = fileName.substring(0, fileName.lastIndexOf("."))+ "@"+size+"x"+size+".jpg";
//            File newFile = new File(fromPic.getParent(), newFileName);
//            builder.outputFormat("jpg").toFile(newFile);
//        } catch (IOException e) {
//            // log.error(fromPic.getPath());
//            e.printStackTrace();
//        }
    }

    private static double getScale(int size, int imageHeightOrWidth) {
        return Math.ceil(size * 10.0 / imageHeightOrWidth) / 10;
    }

}
